﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Win7Helper.Helper;
using Win7Helper.Properties;

namespace Win7Helper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            InitializeComponent();
            // webBrowser1.Navigate(Resource1.cat);
            InitView();
        }

        private int ClickCount=0;
        private void InitView()
        {
            if (!Directory.Exists(StaticValue.ThemeBin1)) {
                MessageBox.Show("未能找到信息文件夹！程序关闭！");
                Environment.Exit(0);
            }
            String text = TextHelper.ReaderText(StaticValue.ThemeInofo1);
            if (text != "") {
                StaticValue.XmlText = text;
                linkLabel1.Text = XmlHelper.GetFirstValue(XmlHelper.SelectNode(StaticValue.XmlText, "userinfo/themepublicity"));
                StaticValue.PublicityLink = XmlHelper.GetFirstValue(XmlHelper.SelectNode(StaticValue.XmlText, "userinfo/themepublicitylink"));

            }
            SetupZero();
            ButtonShow();
        }

        private void ButtonShow()
        {
            if (ClickCount == 0)
                button3.Visible = false;
            else
                button3.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ClickCount++;
            PageJudgment();
           

        }

        private void PageJudgment()
        {
            ButtonShow();
            if (ClickCount == 0)
            {
                SetupZero();

            }else if(ClickCount == 1){
                SetupOne();
            }
            if (ClickCount == 2) {
                SetUpTwo();
            }
            if (ClickCount == 3) {
                ClickCount--;
                if (checkBox1.Checked)
                    SetupFour();
                else
                    MessageBox.Show("你没同意用户许可");
            }
                
        }

        private void SetupFour()
        {
            if (ClickHelper.ThemeInstall())
            {
                GroupBoxReflush(new ThemeWindows.UserBootFour());
                button3.Visible = false;
                checkBox1.Visible = false;
                button1.Visible = false;
            }
            else {
                SetUpError();
            }
        }
        /// <summary>
        /// 设置错误页面
        /// </summary>
        private void SetUpError()
        {
            GroupBoxReflush(new ThemeWindows.UserBootError());
        }

        /// <summary>
        /// 第三页，用户附加协议
        /// </summary>
        private void SetUpTwo()
        {
            GroupBoxReflush(new ThemeWindows.UserBootTwo());
        }

        /// <summary>
        /// 第一个引导页，用户协议
        /// </summary>
        private void SetupOne() {
            GroupBoxReflush(new ThemeWindows.UserBootOne());
        }
        /// <summary>
        /// 开屏引导页
        /// </summary>
        private void SetupZero()
        {
            GroupBoxReflush(new ThemeWindows.UserBootZero());
        }

        private void GroupBoxReflush(Control ReflushView)
        {
            groupBox1.Controls.Clear();
            groupBox1.Controls.Add(ReflushView);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Explorer.exe", ".\\");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ClickHelper.OpenWebBower(StaticValue.PublicityLink);
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ClickCount--;
            PageJudgment();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(".\\Bin\\instatll.themepack");
        }
    }
}
